﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Atacadao.Servico.Entidades;
using Atacadao.Comum.DAL;
using Atacadao.Servico.Query;

namespace Atacadao.Web.Helpers
{
    public class HelperListas
    {
        public static IList<SelectListItem> Recuperar(string classeDados, string codigo, string descricao, string textoSelecione)
        {
            ServicoBase<Fake> servico = new ServicoBase<Fake>();
            List<SelectListItem> itens = new List<SelectListItem>();
            Type t = null;

            if (!string.IsNullOrEmpty(textoSelecione))
            {
                itens.Add(new SelectListItem { Value = "", Text = textoSelecione, Selected = true });
            }

            foreach (var item in servico.Repositorio(classeDados).Select(string.Format("new ({0} as Value, {1} as Text)", codigo, descricao)))
            {
                if (t == null) { t = item.GetType(); }
                itens.Add(new SelectListItem() { Text = t.GetProperty("Text").GetValue(item, null).ToString(), Value = t.GetProperty("Value").GetValue(item, null).ToString() });                
            }

            return itens;
        }


    }
}